package com.ruoyi.web.controller.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.HttpClientUtil;
import com.ruoyi.system.domain.mongodb.*;
import com.ruoyi.system.mongo.AllSchoolDao;
import com.ruoyi.system.mongo.KlassesDao;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.service.mongo.StudentService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author HP
 * @Date 2021/3/19
 * @Description TODO
 */
@RestController
@RequestMapping("/wxRegistered")
public class WxloginRegisteredController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private AllSchoolDao allSchoolDao;
    @Autowired
    private KlassesDao klassesDao;
    @Autowired
    private SysUserDao sysUserDao;
    @RequestMapping("/getUserinfo")
    public AjaxResult getUserinfo(String access_token,String openid){
        //请求获取userInfo
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo" +
                "?access_token=" +access_token+
                "&openid=" +openid+
                "&lang=zh_CN";
        String resultInfo = HttpClientUtil.doGet(infoUrl);
        System.out.println("resultInfo:"+resultInfo);
        return AjaxResult.success(resultInfo);
    }

    @RequestMapping("/queryUsername")
    public String queryUsername(String username){
        Users users = sysUserDao.queryUserByName(username);
        boolean result = true;
        System.out.println(users);
        if (users!=null){
            result = false;
        }
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", result);
        ObjectMapper mapper = new ObjectMapper();
        String resultString = "";
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  resultString;
    }
    @RequestMapping("/registered")
    public AjaxResult registered(Users user){
        try {
            studentService.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }
    @GetMapping("/queryByNames")
    public AjaxResult queryByNames(String name){
        AllSchool allSchool = allSchoolDao.queryByNames(name);
        return AjaxResult.success(allSchool.get_id());
    }
    @GetMapping("/selectSchool")
    public AjaxResult selectSchool(String school){
        List<Klasses> klassesList = klassesDao.selectSchool(school);
        if (klassesList.size()==0){
            return AjaxResult.error();
        }else {
            return AjaxResult.success(klassesList);
        }

    }
}
